import * as types from '../types'
import apis from '../apis'
import logger from '../logger'
import CookieManager from 'react-native-cookies'
import {ENVS} from '../config'



function userLogin(account, password, succesCallback, failCallback) {
    let params = {
        'loginName' : account, 
        'loginPasswd': password
    }
    return dispatch => {
        apis.account.login(params)
            .then(res => {
                // let empRToken = res.obj['empRToken']
                // let empToken = res.obj['empToken']
                // let cookieStr = `empToken=${empToken}; path=/; expires=Thu, 1 Jan 2030 00:00:00 -0000; secure; HttpOnly`
                // CookieManager.setFromResponse('http://121.18.229.58:20001/liveapp/homepage',
                // cookieStr).then((res)=>{
                //     console.log('cookie Manager')
                //     console.log('CookieManager.setFromResponse =>', res);
                // })
                // let empRStr = `empRToken=${empRToken}; path=/; expires=Thu, 1 Jan 2030 00:00:00 -0000; secure; HttpOnly`
                // CookieManager.setFromResponse('http://121.18.229.58:20001/liveapp/homepage',
                // empRStr).then((res)=>{
                //     console.log('cookie Manager')
                //     console.log('CookieManager.setFromResponse =>', res);
                // })
                dispatch({type: types.LOGIN_SUCCESS, account, password});
                if(succesCallback){
                    succesCallback(res)
                }
            })
            .catch(error => {
                console.log(error);
                if(failCallback){
                    failCallback()
                }
            })        
    }
}

export  default{
    userLogin
}